package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.ProvinceBiz;
import com.egao.orderB.entity.Province;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-10-20 15:39:20
 * @version ：1.0
 */
@Repository
public class ProvinceBizImpl extends BaseDao implements ProvinceBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Province province, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Province> rm = new BeanPropertyRowMapper<Province>(Province.class);
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.province_id");
        sql.append(",p.province,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_sys_province p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(province)){
//			if(Utils.isNotEmpty(province.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", province.getName());
//			}
			if(Utils.isNotEmpty(province.getSort())&&Utils.isNotEmpty(province.getOrder())){
                sql.append(" order by p."+province.getSort()+" "+province.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Province> list(Province province, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Province> rm = new BeanPropertyRowMapper<Province>(Province.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.province_id");
        sql.append(",p.province,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_sys_province p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(province)){
//			if(Utils.isNotEmpty(province.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", province.getName());
//			}
            if(Utils.isNotEmpty(province.getSort())&&Utils.isNotEmpty(province.getOrder())){
				sql.append(" order by p."+province.getSort()+" "+province.getOrder());
			} else {
				sql.append(" order by p.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Province getById(Province province) {
		RowMapper<Province> rm = new BeanPropertyRowMapper<Province>(Province.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.province_id");
        sql.append(",p.province,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_sys_province p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 and p.id=:id ");
		map.put("id", province.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Province province, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_sys_province (");
        sql.append("organization_id,province_id,province,create_user,create_time) values ( ");
        sql.append(":organization_id,:province_id,:province,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("province_id", province.getProvinceId());
        map.put("province", province.getProvince());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Province province, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_sys_province set ");
        sql.append("province_id=:province_id,province=:province,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("province_id", province.getProvinceId());
        map.put("province", province.getProvince());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", province.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Province province, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(province.getId().length()>0){
			for(String ID : province.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_sys_province set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
